﻿using System;
using System.Data;
using ebay.Utility;
using System.Data.SqlClient;

namespace ebay.Dal
{
    public class Pagination
    {
        public Pagination()
        {

        }

        public DataTable GetData(int page, int pageSize, string TableName, string Field, string OrderBy, string Filter, ref int MaxPage, ref int TotalRow, ref string Descript)
        {
            SqlParameter[] paraslist = { 
                                   new SqlParameter("@Page",SqlDbType.Int),
                                   new SqlParameter("@PageSize",SqlDbType.Int),
                                   new SqlParameter("@Table",SqlDbType.VarChar,65535),
                                   new SqlParameter("@Field",SqlDbType.VarChar,65535),
                                   new SqlParameter("@OrderBy",SqlDbType.VarChar,100),
                                   new SqlParameter("@Filter",SqlDbType.VarChar,65535),
                                   new SqlParameter("@MaxPage",SqlDbType.Int),
                                   new SqlParameter("@TotalRow",SqlDbType.Int),
                                   new SqlParameter("@Descript",SqlDbType.VarChar,65535)
                                   };
            paraslist[0].Value = page;
            paraslist[1].Value = pageSize;
            paraslist[2].Value = TableName;
            paraslist[3].Value = Field;
            paraslist[4].Value = OrderBy;
            paraslist[5].Value = Filter;
            paraslist[6].Direction = ParameterDirection.Output;
            paraslist[7].Direction = ParameterDirection.Output;
            paraslist[8].Direction = ParameterDirection.Output;
            DataTable tableSource = SqlHelper.ExecuteDataTable(SqlHelper.ceterDB_Conn, CommandType.StoredProcedure, "sp_Pagination", paraslist);
            MaxPage = paraslist[6].Value == DBNull.Value || paraslist[6].Value == null ? 0 : Convert.ToInt32(paraslist[6].Value);
            TotalRow = paraslist[7].Value == DBNull.Value || paraslist[7].Value == null ? 0 : Convert.ToInt32(paraslist[7].Value);
            Descript = paraslist[8].Value == DBNull.Value || paraslist[8].Value == null ? "" : paraslist[8].Value.ToString();
            if (!string.IsNullOrEmpty(Descript))
            {
                throw new Exception(Descript);
            }
            return tableSource;
        }

        public DataTable GetCommonData(string TableName,string PrimaryKey, string Field,int PageSize,int CurrentPage,string Filter,string GroupBy,string OrderBy,ref int TotalRow)
        {
            SqlParameter[] paraslist = { 
                                   new SqlParameter("@TableNames",SqlDbType.VarChar),
                                   new SqlParameter("@PrimaryKey",SqlDbType.VarChar),
                                   new SqlParameter("@Fields",SqlDbType.VarChar,8000),
                                   new SqlParameter("@PageSize",SqlDbType.Int),
                                   new SqlParameter("@CurrentPage",SqlDbType.Int),
                                   new SqlParameter("@Filter",SqlDbType.VarChar,8000),
                                   new SqlParameter("@Group",SqlDbType.VarChar),
                                   new SqlParameter("@Order",SqlDbType.VarChar),
                                   new SqlParameter("@Count",SqlDbType.Int)
                                   };
            paraslist[0].Value = TableName;
            paraslist[1].Value = PrimaryKey;
            paraslist[2].Value = Field;
            paraslist[3].Value = PageSize;
            paraslist[4].Value = CurrentPage;
            paraslist[5].Value = Filter;
            paraslist[6].Value = GroupBy;
            paraslist[7].Value = OrderBy;
            paraslist[8].Direction = ParameterDirection.Output;
            DataTable tableSource = SqlHelper.ExecuteDataTable(SqlHelper.ceterDB_Conn, CommandType.StoredProcedure, "sp_CommonPage", paraslist);
            TotalRow = paraslist[8].Value == DBNull.Value || paraslist[7].Value == null ? 0 : Convert.ToInt32(paraslist[8].Value);
            return tableSource;
        }
    }
}